package com.xyw.code.controller;


import com.xyw.code.constant.CommonStatusEnum;
import com.xyw.code.entity.SysJob;
import com.xyw.code.log.SysLog;
import com.xyw.code.service.ISysJobService;
import com.xyw.code.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 岗位管理 前端控制器
 * </p>
 *
 * @author xuyiwei
 * @since 2019-07-30
 */
@Api(value="岗位模块")
@RestController
@RequestMapping("/job")
public class SysJobController {
    @Autowired
    private ISysJobService jobService;


    @GetMapping("/listByDeptId/{deptId}")
    public Result<List<SysJob>> selectJobListByDeptId(@PathVariable("deptId") Integer deptId) {
        return Result.ok(jobService.selectJobListByDeptId(deptId));
    }

    /**
     * 获取岗位列表
     * @param page
     * @param pageSize
     * @param jobName
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:job:view')")
    public Result getList(Integer page, Integer pageSize,@RequestParam(defaultValue = "") String jobName) {
        return Result.ok(jobService.selectJobList(page, pageSize, jobName));
    }

    /**
     * 保存岗位
     *
     * @param sysJob
     * @return
     */
    @SysLog(descrption = "新增岗位")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:job:add')")
    public Result add(@RequestBody SysJob sysJob) {
        return Result.ok(jobService.save(sysJob));
    }

    /**
     * 更新岗位
     * @param sysJob
     * @return
     */
    @SysLog(descrption = "更新岗位")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('sys:job:update')")
    public Result update(@RequestBody SysJob sysJob) {
        return Result.ok(jobService.updateById(sysJob));
    }

    /**
     * 根据id删除岗位
     * @param id
     * @return
     */
    @SysLog(descrption = "删除岗位")
    @PostMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:job:delete')")
    public Result delete(@PathVariable("id") Integer id) {
        SysJob job = new SysJob();
        job.setId(id);
        job.setStatus(CommonStatusEnum.DELETE.getType());
        return Result.ok(jobService.updateById(job));
    }
}

